package com.etl.test;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * REST同步单例类
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-12-10 15:59
 */
public class RestSyncSingleton {

    public static final String QUESTION_MARK = "?";
    public static final String AMPERSAND = "&";
    public static final String EQUALS_TO = "=";
    public static final String BRACE_LEFT = "{";
    public static final String BRACE_RIGHT = "}";

    /**
     * HTTP请求头参数
     */
    private final HttpHeaders httpHeaders;

    /**
     * REST请求模板
     */
    private final RestTemplate restTemplate;

    /**
     * 内部静态类，实现单例
     */
    private static class Holder {
        public static final RestSyncSingleton INSTANCE = new RestSyncSingleton();
    }

    /**
     * 实例化
     *
     * @return INSTANCE
     */
    private static RestSyncSingleton getInstance() {
        return Holder.INSTANCE;
    }

    /**
     * 私有构造方法
     */
    private RestSyncSingleton() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(60000);
        requestFactory.setReadTimeout(120000);
        this.restTemplate = new RestTemplate(requestFactory);
        this.httpHeaders = new HttpHeaders();
        this.httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        this.httpHeaders.add(HttpHeaders.ACCEPT, MediaType.ALL_VALUE);
    }


    public String get() {
        String url = "http://10.180.7.5:2303/json/queryCinemaAndSessions";
        Map<String, Object> uriVariables = new HashMap<>();
        uriVariables.put("tenantId", "321566");
        uriVariables.put("uuid", "123456");
        uriVariables.put("usbKey", "123456");
        uriVariables.put("cinemaCode", "11050611");
        uriVariables.put("startDataTime", "2020-12-10 11:08:00");
        uriVariables.put("endDataTime", "2020-12-10 23:00:00");

        StringBuffer urlBuffer = new StringBuffer(StringUtils.trimToEmpty(url));
        if(uriVariables != null && !uriVariables.isEmpty()){
            if(!StringUtils.endsWith(urlBuffer, QUESTION_MARK)){
                urlBuffer.append(QUESTION_MARK);
            }
            Iterator<String> iterator = uriVariables.keySet().iterator();
            while (iterator.hasNext()){
                String key = iterator.next();
                urlBuffer.append(key);
                urlBuffer.append(EQUALS_TO);
                urlBuffer.append(BRACE_LEFT);
                urlBuffer.append(key);
                urlBuffer.append(BRACE_RIGHT);
                urlBuffer.append(AMPERSAND);
            }
        }
        if(StringUtils.endsWith(urlBuffer, QUESTION_MARK) || StringUtils.endsWith(urlBuffer, AMPERSAND)){
            urlBuffer.deleteCharAt(urlBuffer.length() - 1);
        }
        String response = null;
        HttpEntity<String> httpEntity = new HttpEntity<String>(httpHeaders);
        long startTime = System.currentTimeMillis();
        try {
            response = restTemplate.exchange(urlBuffer.toString(),
                    HttpMethod.GET, httpEntity, String.class, uriVariables).getBody();
        } catch (Exception t) {
            System.out.println(t.getMessage());
        } finally {

        }
        return response;
    }

    public static void main(String[] args) {
//        String response = RestSyncSingleton.getInstance().get();
//        System.out.println(response);
        String str = null;
        System.out.println(false);
    }
}
